﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

using BusinessManagementStudio.Models.BusinessObject.Entities;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;

namespace BusinessManagementStudio.Models.BusinessObject.Services
{
    public class ContractorService : EntityService<Contractor>
    {
        public List<Contractor> GetAllContractors()
        {
            return (from contractor in this.MongoConnectionHandler.MongoCollection.FindAllAs<Contractor>()
                    where contractor.IsValid == true
                    select contractor).ToList();
        }

        private List<SelectListItem> GetContractorsList()
        {
            List<SelectListItem> ContractorsList = new List<SelectListItem>();

            List<Contractor> AllContractorsList = GetAllContractors().OrderBy(x => x.ContractorID).ToList();

            foreach (Contractor contractor in AllContractorsList)
            {
                SelectListItem sListItem = new SelectListItem();
                sListItem.Text = contractor.ContractorName;
                sListItem.Value = contractor.ContractorCode.ToString();

                ContractorsList.Add(sListItem);
            }

            return ContractorsList;
        }

        public IEnumerable<SelectListItem> GetContractorNameList()
        {
            return GetContractorsList();
        }

        public Contractor GetContractorByContractorCode(String ContractorCode)
        {
            return (from contractor in this.MongoConnectionHandler.MongoCollection.FindAllAs<Contractor>()
                    where contractor.ContractorCode == ContractorCode
                    select contractor).FirstOrDefault();
        }

        public override void Update(Contractor entity)
        {
            //// Not necessary for the example
        }
    }
}